'''
Created on Sep 16, 2012

@author: kingsfield
'''
from collections import defaultdict
#from new_bayes_skus import block_size, block, MAX_BLOCK, get_time_idx, get_date

#trainfile = '/home/kingsfield/Desktop/BestBuyPc/old_train.csv'
#testfile = '/home/kingsfield/Desktop/BestBuyPc/old_test.csv'

trainfile = '/home/kingsfield/Desktop/BestBuyPc/fast_correct_train_small.csv'
testfile = '/home/kingsfield/Desktop/BestBuyPc/fast_correct_test.csv'

with open(testfile) as fr:
    raw_data = fr.readlines()
uids = [line.split(',')[0] for line in raw_data]
test_uid = set(uids)

with open(trainfile) as fr:
    raw_data = fr.readlines()
uids = [line.split(',')[0] for line in raw_data]
train_uid = set(uids)
uid_count = defaultdict(int)
for uid in uids:
    uid_count[uid] += 1
rank = sorted(uid_count.items(), key=lambda x:x[1], reverse=True)
hot_uid = set([i[0] for i in rank if i[1] >= 1])
target_uid = hot_uid.intersection(test_uid)

query_dict = dict()
#time_item_count = defaultdict(lambda:defaultdict(lambda: defaultdict(int)))
for line in raw_data:
    #user, sku, category, raw_query, click_time, ___query_time = line.split(',')
    user, sku, category, raw_query, click_time = line.split(',')
    if user not in query_dict:
        query_dict[user] = dict()
    if raw_query not in query_dict[user]:
        query_dict[user][raw_query] = list()
    query_dict[user][raw_query].append(sku)
    
#    t = get_date(click_time)
#    time_block = get_time_idx(t)
#    time_item_count[time_block][category][sku] += 1

def get_duplicate_query(user, raw_query):
    if user in query_dict and raw_query in query_dict[user]:
        return query_dict[user][raw_query]
    else:
        return []
